package com.simple.indiapay.controller;

import com.google.common.collect.Sets;
import com.simple.common.entity.ResData;
import com.simple.common.entity.constant.Payment;
import com.simple.common.entity.controller.CallbackDynamicAction;
import com.simple.common.entity.dto.pay.CallbackDynamicDto;
import com.simple.common.entity.dto.pay.CallbackRspDto;
import com.simple.common.entity.po.sys.Trade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author yasud
 * @date 2021年11月06日 18:49
 * @description
 */
@RestController
public class CallbackController implements CallbackDynamicAction {

    private final String SUCCESS_CODE = "00";

    @Override
    public ResData callbackDynamic(CallbackDynamicDto callbackDto) throws Exception {
        Trade trade = callbackDto.getTrade();
        CallbackRspDto rspDto = new CallbackRspDto();
        if (trade.getType() == Trade.Type.NORMAL_TRADE) {
            Map<String, Object> param = callbackDto.getParam();
            String returnCode = (String) param.get("returncode");
            String transactionId = param.get("transaction_id") + "";
            String refMsg = param.getOrDefault("refMsg", "未知错误") + "";
            if (StringUtils.equalsIgnoreCase(SUCCESS_CODE, returnCode)) {
                trade.setStatus(Trade.Status.SUCCESS);
                trade.setMsg(Payment.TRADE_STATUS_DESCRIBE.EVEN_SERIAL_STATUS_PAYMENT_SUCCESS_MSG);
                trade.setChannelStatus(returnCode);
                trade.setChannelMsg(Payment.TRADE_STATUS_DESCRIBE.EVEN_SERIAL_STATUS_PAYMENT_SUCCESS_MSG);
                trade.setChannelOrderNo(transactionId);
            } else {
                trade.setStatus(Trade.Status.FAILED);
                trade.setMsg(Payment.TRADE_STATUS_DESCRIBE.EVEN_SERIAL_STATUS_PAYMENT_FAILURE_MSG);
                trade.setChannelStatus(returnCode);
                trade.setChannelMsg(refMsg);
                trade.setChannelOrderNo(transactionId);
            }
        } else {
            Map<String, Object> param = callbackDto.getParam();
            String refCode = "" + param.get("refCode");
            String transactionId = param.get("transaction_id") + "";
            String refMsg = param.getOrDefault("refMsg", "未知错误") + "";
            if (StringUtils.equalsIgnoreCase("1", refCode)) {
                trade.setStatus(Trade.Status.SUCCESS);
                trade.setMsg(Payment.TRADE_STATUS_DESCRIBE.EVEN_SERIAL_STATUS_PAYMENT_SUCCESS_MSG);
                trade.setChannelStatus(refCode);
                trade.setChannelMsg(Payment.TRADE_STATUS_DESCRIBE.EVEN_SERIAL_STATUS_PAYMENT_SUCCESS_MSG);
                trade.setChannelOrderNo(transactionId);
            }
            else if (Sets.newHashSet("3", "4", "5", "6", "8").contains(refCode)) {
                trade.setStatus(Trade.Status.PENDING);
                trade.setMsg(Payment.TRADE_STATUS_DESCRIBE.EVEN_SERIAL_STATUS_PAYMENT_PENDING_MSG);
                trade.setChannelStatus(refCode);
                trade.setChannelMsg(Payment.TRADE_STATUS_DESCRIBE.EVEN_SERIAL_STATUS_PAYMENT_PENDING_MSG);
                trade.setChannelOrderNo(transactionId);
            }
            else {
                trade.setStatus(Trade.Status.FAILED);
                trade.setMsg(Payment.TRADE_STATUS_DESCRIBE.EVEN_SERIAL_STATUS_PAYMENT_FAILURE_MSG);
                trade.setChannelStatus(refCode);
                trade.setChannelMsg(refMsg);
                trade.setChannelOrderNo(transactionId);
            }
        }
        rspDto.setTrade(trade);
        return ResData.ok(rspDto);
    }
}
